var g = function (id){
    if( id.substr(0,1) == '.'){
        return document.getElementsByClassName(id.substr(1));
    }
    return document.getElementById(id);
};
var mlist = [
    {id:'m1',img:'editor',title:'实验室编辑器',link:'javascript:void(0)'},
    {id:'m2',img:'ilang',title:'MarkDown简明语法',link:'./markdown.html'},
    {id:'m3',img:'icloud',title:'在线查看MD文件',link:'./mdwiki'},
    {id:'m4',img:'ieditor',title:'editor.md编辑器',link:'./editor/index'},
    {id:'m5',img:'isvg',title:'SVG在线编辑器',link:'./svg-editor'},
    {id:'m6',img:'#',title:'网页设计色板图',link:'./webpage-design-color.html'},
    {id:'m7',img:'icode',title:'CodeMirror编辑器',link:'./cm'},
    {id:'m8',img:'#',title:'showdownjs翻译器',link:'https://github.com/showdownjs/showdown'},
    {id:'m9',img:'#',title:'公开文件列表',link:'public.php'}
];
function addList(){
    var list=g('list').innerHTML.replace(/^\s*/,'').replace(/^\s*/,'');
    var out_list= [];
    for(i in mlist){
        var _html_list=list.replace(/{{title}}/g,mlist[i].title);
        if(mlist[i].img!='#'){
            _html_list = _html_list.replace(/{{img}}/g,mlist[i].img);
        }
        _html_list = _html_list.replace(/{{link}}/g,mlist[i].link).replace(/{{label}}/g,mlist[i].id);
        out_list.push(_html_list);
    }
    g('list').innerHTML = out_list.join('');
};
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
};
function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
};
$(document).ready(function(){
    addList();
    $('#m1').click(function(){
        if(getCookie('username')!=''){
            window.location.href='myfile.php';
        }else{
            alert('请登录后创建新文件再查看！') ;
        }       
    });
})